<?php

class VarsBackend extends Backend{

	public static $modName = "Контакты";
	private $tpl_path;

	public function __construct(){
		$this->tpl_path = dirname(__FILE__) . '/tpl/';
	}

	function out(){
		$out = '';

		$tpl = file_get_contents($this->tpl_path . 'var_form.htm');

		$d = dir(dirname(__FILE__));

		while (false !== ($entry = $d->read())) {

			if($entry == '.' || $entry == '..' || !is_file(dirname(__FILE__) . '/' . $entry)
				|| $entry == 'backend.php' || $entry == 'out.php'){
				continue;
			}

			$className = explode('.', $entry);

			if(class_exists($className[0], true) && in_array("getInstanse", get_class_methods($className[0]))){

				$class = call_user_func(array($className[0], 'getInstanse'));

				if($class instanceof VarsInterface ){

					$class->backendSave();
					$out .= strtr($tpl,
						array(
							'{NAME}' => $class->getName(),
							'{FORM}' => $class->getBackendOut(),
						)
					);
				}

			}

		}

		return $out;
	}

	public static function parseVars($found){

		if(isset($found[1]) && class_exists($found[1])){

			$class = call_user_func(array($found[1], 'getInstanse'));

			if($class instanceof VarsInterface ){

				return $class->out();
			}
		}
		return $found[0];
	}

}